package com.tarena.lbs.activity.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.query.ActivityQueryPage;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ActivityRepositoryImpl implements ActivityRepository {
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public PageInfo<ActivityPO> pagesList(ActivityQueryPage query) {
        QueryWrapper<ActivityPO> queryWrapper = assembleAdminQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<ActivityPO> pos = activityMapper.selectList(queryWrapper);

        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(pos);
//        // 创建一个查询条件包装器
//        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
//// 如果查询参数中的businessId不为空，则添加查询条件，使business_id字段等于查询参数中的businessId
//        if (query.getBusinessId()!=null){
//            queryWrapper.eq("business_id",query.getBusinessId());
//        }
//// 开启分页，根据查询参数中的页码和页面大小进行设置
//
//        log.debug("开始查询活动列表，查询参数：{}", query);
//// 使用上述查询条件和分页设置，从activityMapper中选择符合条件的活动列表
//        PageHelper.startPage(query.getPageNo(), query.getPageSize());
//        List<ActivityPO> pos = activityMapper.selectList(queryWrapper);
//// 将查询结果封装成PageInfo对象并返回，以便于后续处理和展示
//        return new PageInfo<>(pos);

    }

    @Override
    public ActivityPO selectById(Integer id) {
        return activityMapper.selectById(id);
    }

    private QueryWrapper<ActivityPO> assembleAdminQuery(ActivityQueryPage query) {
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        //1.accountPhone 账号手机号 相等查询 where account_phone=#{phone}
        if (StringUtils.isNotBlank(query.getActivityType())){
            queryWrapper.eq("activity_type",query.getActivityType());
        }
        //2.nickname 昵称 模糊查询 非空 where nickname like(%#{}%)
        if (StringUtils.isNotBlank(query.getActivityName())){
            queryWrapper.like("activity_name",query.getActivityName());
        }
        return queryWrapper;
    }
}
